package com.yikaiye.android.yikaiye.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.databinding.DialogDownloadBinding;
import com.yikaiye.android.yikaiye.utils.ScreenUtil;

/**
 * 下载进度条弹窗
 */
public class DownloadDialog {
    private final Dialog dialog;
    private final ProgressBar progressBar;

    public DownloadDialog(Context context) {
        DialogDownloadBinding binding = DialogDownloadBinding.inflate(LayoutInflater.from(context));
        dialog = new Dialog(context, R.style.prompt_progress_dialog_dimenabled);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ScreenUtil.getMaxWidth(context) - ScreenUtil.dip2px(40),
                LinearLayout.LayoutParams.MATCH_PARENT);
        dialog.setContentView(binding.getRoot(), layoutParams);
        dialog.setCancelable(false);
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
        }
        progressBar = binding.progressBar;
    }

    public void setProgress(int progress) {
        progressBar.setProgress(progress);
    }

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
